/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AutomatonCanvas.java
 *
 * Created on Oct 30, 2010, 2:21:01 PM
 */
package jgaf.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import jgaf.automaton.fa.AutomatonTableEditor;
import jgaf.editor.Editor;
import jgaf.automaton.fa.StateDiagramEditor;
import jgaf.environment.PropertiesHandler;

/**
 *
 * @author hanis
 */
public class AutomatonTableRepresenter extends JPanel implements MouseListener, MouseMotionListener, KeyListener {

    private JScrollPane scroller = null;
    private AutomatonTableEditor editor;

    public AutomatonTableRepresenter(AutomatonTableEditor editor) {
        this.editor = editor;
        addMouseListener(this);
        addMouseMotionListener(this);
        addKeyListener(this);
        initComponents();
        initMyComponents();
    }


    private void initMyComponents() {
        setVisible(true);
        setFocusable(true);
        requestFocus();
    }


    @Override
    public void paintComponent(Graphics g) {
        setBackground(PropertiesHandler.getInstance().getAutomatonCanvasBackground());
        super.paintComponent(g);        
        if (editor != null) {
                editor.paint((Graphics2D) g);
                revalidate();
        }
    }


    
    @Override
    public Dimension getPreferredSize() {
        return editor.getPreferredSize();
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(255, 255, 255));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        //editor.centerAndScaleGraphics(true);
        //   System.out.println("!!!");
     //   editor.centerAndScaleGraphics(this.getSize());
    }//GEN-LAST:event_formComponentResized

    public void mousePressed(MouseEvent e) {
        if (editor != null && editor.isEditable()) {
            if (e.getButton() == java.awt.event.MouseEvent.BUTTON1) {
                editor.handleMouseLeftPressed(e);
            }
        }
    }

    public void mouseReleased(MouseEvent e) {
        if (editor != null && editor.isEditable()) {
            if (e.getButton() == java.awt.event.MouseEvent.BUTTON1) {
                editor.handleMouseLeftReleased(e);
            }
        }
    }

    public void mouseEntered(MouseEvent e) {
        requestFocus();
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
        if (editor != null && editor.isEditable()) {
            if (e.getButton() == java.awt.event.MouseEvent.BUTTON3) {
                editor.handleRightMouseClick(e);
            }
        }
    }

    public void mouseDragged(MouseEvent e) {
        if (editor != null && editor.isEditable()) {
            editor.handleMouseDragged(e);
        }
    }

    public void mouseMoved(MouseEvent e) {
        if (editor != null && editor.isEditable()) {
            editor.handleMouseMoved(e);
        }
    }

    public void keyTyped(KeyEvent e) {

    }

    public void keyPressed(KeyEvent e) {
        if (editor != null && editor.isEditable()) {
            editor.handleKeyPressed(e);
        }
    }

    public void keyReleased(KeyEvent e) {
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
